package com.cloud.pole.weixin.service.impl;

import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.pole.weixin.biz.WxPayNotifyBiz;
import com.cloud.pole.weixin.model.entity.WxPayNotify;
import com.cloud.pole.weixin.service.WxPayNotifyService;

import lombok.extern.slf4j.Slf4j;

/*
*2019年4月24日
*cloud-weixin
*author:frank-chen
*/
@Service
@Slf4j
public class WxPayNotifyServiceImpl implements WxPayNotifyService {
	
	@Autowired
	private WxPayNotifyBiz wxPayNotifyBiz;
	
	@Override
	public void payWxNotify(SortedMap<Object, Object> packageParams) throws Exception {
		// TODO Auto-generated method stub
		String appid = (String) packageParams.get("appid");
		String mchId = (String) packageParams.get("mch_id");
		String deviceInfo = (String) packageParams.get("device_info");
		String nonceStr = (String) packageParams.get("nonce_str");
		String sign = (String) packageParams.get("sign");
		String resultCode = (String) packageParams.get("result_code");
		String errCode = (String) packageParams.get("err_code");
		String errCodeEes = (String) packageParams.get("err_code_des");
		String openid = (String) packageParams.get("openid");
		String isSubscribe = (String) packageParams.get("is_subscribe");
		String tradeType = (String) packageParams.get("trade_type");
		String bankType = (String) packageParams.get("bank_type");
		String totalFee = (String) packageParams.get("total_fee");
		String couponFee = (String) packageParams.get("coupon_fee");
		String feeType = (String) packageParams.get("fee_type");
		String transactionId = (String) packageParams.get("transaction_id");
		String outTradeNo = (String) packageParams.get("out_trade_no");
		String timeEnd = (String) packageParams.get("time_end");
		String attach = (String) packageParams.get("attach");
		if(couponFee == null || couponFee.equals("")){
			couponFee = "0";
		}
		if(totalFee == null || totalFee.equals("")){
			totalFee = "0";
		}
		WxPayNotify weiXinPayNotify = new WxPayNotify();
		weiXinPayNotify.setAppid(appid);
		weiXinPayNotify.setBankType(bankType);
		weiXinPayNotify.setCouponFee(Integer.parseInt(couponFee));
		weiXinPayNotify.setCreateTime(new Date());
		weiXinPayNotify.setDeviceInfo(deviceInfo);
		weiXinPayNotify.setErrCode(errCode);
//		weiXinPayNotify.setErrCodeEes(errCodeEes);
		weiXinPayNotify.setFeeType(feeType);
		weiXinPayNotify.setIsSubscribe(isSubscribe);
		weiXinPayNotify.setMchId(mchId);
		weiXinPayNotify.setNonceStr(nonceStr);
		weiXinPayNotify.setOpenid(openid);
		weiXinPayNotify.setOutTradeNo(outTradeNo);
		weiXinPayNotify.setResultCode(resultCode);
		weiXinPayNotify.setSign(sign);
		weiXinPayNotify.setTimeEnd(timeEnd);
		weiXinPayNotify.setTotalFee(Integer.parseInt(totalFee));
		weiXinPayNotify.setTradeType(tradeType);
		weiXinPayNotify.setTransactionId(transactionId);
		weiXinPayNotify.setAttach(attach);
		wxPayNotifyBiz.insertSelective(weiXinPayNotify);
	}
}
